package org.artifact.xls.convert.file;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import org.artifact.xls.ExcelExport;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class AbstractFileConvert {
    private static final Log log = LogFactory.get(AbstractFileConvert.class);
    public static final String TYPE_STRING = "str";
    public static final String TYPE_STRING_ARRAY = "strs";
    public static final String TYPE_INT = "int";
    public static final String TYPE_INT_ARRAY = "ints";
    public static final String TYPE_JSON = "json";
    public static final String TYPE_MAP = "map";
    public static final String TYPE_TXT = "txt";


    protected static Map<String,String> i18n = new HashMap<>();

    /**
     * 输出Excle
     * @param file
     * @param outputPath
     */
    public void convertXlsx(File file, String outputPath){
        String type = FileUtil.getType(file);

        ExcelReader reader = ExcelUtil.getReader(file);

        reader.setIgnoreEmptyRow(true);
        if (ExcelExport.SHEET_MODE){
            List<String> sheetNames = reader.getSheetNames();
            for (String sheetName : sheetNames) {
                reader.setSheet(sheetName);
                String[] strs = StrUtil.split(sheetName,"|");
                if (strs.length != 2){
                    continue;
                }
                log.debug("export {}",StrUtil.upperFirst(strs[0]));
                onConvertXlsx(StrUtil.upperFirst(strs[0]),reader,outputPath);
            }
        }else{
            String fileName = FileUtil.mainName(file);
            String[] strs = StrUtil.split(fileName,"|");
            if (strs.length != 2){
                return;
            }
            log.debug("export {}",StrUtil.upperFirst(strs[0]));
            onConvertXlsx(StrUtil.upperFirst(strs[0]),reader,outputPath);
        }
    }


    public abstract void onConvertXlsx(String fileName,ExcelReader reader, String outputPath);

    /**
     * 输出Yml
     * @param file
     * @param outputPath
     */
    public abstract void convertYml(File file, String outputPath);

    /**
     * 文件夹
     * @return
     */
    public abstract String getDir();

    /**
     * 输出i18n文件
     * @param outputPath
     */
    public abstract void writeI18nFile(String outputPath);
}
